package FourArithmeticOperations;

import java.util.ArrayList;
import java.util.Iterator;


public class LinkedBinaryTree<T> implements BinaryTree<T>
{
    protected BTNode<T> root;

    public LinkedBinaryTree()
    {
        root = null;
    }

    public LinkedBinaryTree (T element)
    {
        root = new BTNode<T>(element);
    }

    public LinkedBinaryTree (T element, LinkedBinaryTree<T> left,
                             LinkedBinaryTree<T> right)
    {
        root = new BTNode<T>(element);
        root.setLeft(left.root);
        root.setRight(right.root);
    }

    public T getRootElement()
    {
        return root.getElement();
    }

    public LinkedBinaryTree<T> getLeft()
    {
        LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
        result.root = root.getLeft();

        return result;
    }

    public T find (T target)
    {
        BTNode<T> node = null;

        if (root != null)
            node = root.find(target);
        return node.getElement();
    }

    public int size()
    {
        int result = 0;

        if (root != null)
            result = root.count();

        return result;
    }

    public ArrayList<T> inorder()
    {
        ArrayList<T> iter = new ArrayList<>();

        if (root != null)
            root.inorder (iter);

        return iter;
    }

    public ArrayList<T> levelorder()
    {
        LinkedQueue<BTNode<T>> queue = new LinkedQueue<BTNode<T>>();
        ArrayList<T> iter = new ArrayList<>();

        if (root != null)
        {
            queue.enqueue(root);
            while (!queue.isEmpty())
            {
                BTNode<T> current = queue.dequeue();

                iter.add (current.getElement());

                if (current.getLeft() != null)
                    queue.enqueue(current.getLeft());
                if (current.getRight() != null)
                    queue.enqueue(current.getRight());
            }
        }

        return iter;
    }


    public LinkedBinaryTree<T> getRight() {
        LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
        result.root = root.getRight();

        return result;
    }
    public boolean contains (T target) {
        boolean result = false;
        if (target.equals(find(target)))
            result =true;
        System.out.println(result);
        return result;
    }
    public boolean isEmpty() {
        boolean result=true;
        if (root.count()==0)
            return result = false;
        return result;
    }

     public ArrayList<T> preorder() {
         ArrayList<T> iter = new ArrayList<>();

         if (root != null)
             root.preorder (iter);

         return iter;
     }
     public ArrayList<T> postorder() {
         ArrayList<T> iter = new ArrayList<>();

         if (root != null)
             root.postorder (iter);

         return iter;
     }
}